<?php
/**
 * @package if
 */

//检查PHP版本
if (version_compare(PHP_VERSION, "5.0.0") < 0) {
	exit("I-Framework must runs on PHP >= 5.0.0 (current PHP version on your server is " . PHP_VERSION . ")");
}

//框架版本
define("IF_VERSION", "1.1.2");

//框架位置
define("IF_FW_DIR", dirname(__FILE__));

//定义nil
define("nil", "nil_" . uniqid(microtime(true)));

//常量
if (isset($_SERVER["REQUEST_URI"])) {
    define("__URI__", $_SERVER["REQUEST_URI"]);
}

//选项
//error_reporting(E_ALL);
set_magic_quotes_runtime(0);

//加载底层文件
require(IF_FW_DIR . "/vm/functions.php");

//对访问环境的支持
if (!defined("IF_REWRITED")) {
	define("IF_REWRITED", if_rewrite());
}
$GLOBALS["__BASE__"] = rtrim(dirname(if_server("SCRIPT_NAME")), DIRECTORY_SEPARATOR);
$GLOBALS["__ROUTER__"] = IF_REWRITED?$GLOBALS["__BASE__"]:if_server("SCRIPT_NAME");

//路径
$realpath = if_server("PATH_INFO");
$realpath = str_replace("\\", "/", $realpath);
$realpath = preg_replace("{/+}", "/", $realpath);
if (in_array($realpath, array("/favicon.ico", "/robots.txt"))) {
	exit();
}
define("__PATH__", $realpath);
$GLOBALS["IF_ACTION_PATH"] = $realpath;

//分析组件
if_parse_component();
define("IF_ACTION_PATH", preg_replace("/\.\w+$/", "", $GLOBALS["IF_ACTION_PATH"]));
define("__BASE__", $GLOBALS["__BASE__"]);
define("__ROUTER__", $GLOBALS["__ROUTER__"]);

//加载环境配置
if (!defined("IF_APP_ENV")) {
	define("IF_APP_ENV", "dev");
}
if (in_array(IF_APP_ENV, array("dev", "prod", "test"))) {
	require(IF_FW_DIR . "/config/_env_" . IF_APP_ENV . ".php");
}
else {
	exit("IF_APP_ENV value '" . IF_APP_ENV . "' is wrong, should be one of 'dev','prod' and 'test'");
}

//开启编译器
if (defined("IF_COMPILER_ENABLED") && IF_COMPILER_ENABLED) {
	if_start_compiler(IF_COMPILER_DIR);
}

//加载核心文件
if (!class_exists("IApplication", false)) {
	require(IF_FW_DIR . "/vm/IVM.php");
	require(IF_FW_DIR . "/core/IObject.php");
	require(IF_FW_DIR . "/core/IApplication.php");
	require(IF_FW_DIR . "/core/IConfig.php");
	require(IF_FW_DIR . "/util/error/IErrorHandler.php");
}

//加载函数库
if (!defined("IF_UTIL_FUNCTIONS_LOADED")) {
	require(IF_FW_DIR . "/util/functions/functions.php");
}

//程序结束回调
if (function_exists("register_shutdown_function")) {
	register_shutdown_function("if_shutdown");
}

?>